package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.ProductAttribute;
import com.zrrd.yunchmall.product.entity.ProductCategory;
import com.zrrd.yunchmall.product.service.IProductAttributeService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/productAttribute")
@Api(tags = "商品服务-商品属性模块")
public class ProductAttributeController {
    @Autowired
    private IProductAttributeService productAttributeService;

    @ApiOperation("查询商品类型")
    @GetMapping("/list/{id}")
    public ResponseResult list(@PathVariable("id") int id,@RequestParam int pageSize,
                               @RequestParam int pageNum, Integer type){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type",type);
        return new ResponseResult<>(200,"查询成功!",productAttributeService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @ApiOperation("查询商品类型")
    @GetMapping("/{id}")
    public ResponseResult list(@PathVariable("id") int id){
        return new ResponseResult<>(200,"查询成功!",productAttributeService.getById(id));
    }
    @ApiOperation("查询商品类型")
    @PostMapping("/update/{id}")
    public ResponseResult edit(@PathVariable("id") int id,@RequestBody ProductAttribute productAttribute){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200,"查询成功!",productAttributeService.update(productAttribute,updateWrapper));
    }
    @ApiOperation("添加商品属性")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody ProductAttribute productAttribute){
        return new ResponseResult<>(200,"添加成功!",productAttributeService.save(productAttribute));
    }
    @ApiOperation("批量删除商品属性")
    @PostMapping("/delete")
    public ResponseResult deleteByIds(String ids){
        for (String id:ids.split(",")) {
            productAttributeService.removeById(Long.valueOf(id));
        }
        return new ResponseResult<>(200,"删除成功!");
    }
    @ApiOperation("查询商品类型")
    @GetMapping("/attrInfo/{id}")
    public ResponseResult get(@PathVariable("id") int id){
        return new ResponseResult<>(200,"查询成功!",productAttributeService.getById(id));
    }
}
